<?php if(!defined('BASEPATH')) exit('No direct script access allowed');

class InvSo extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->common_model->checkpurview();
        $this->jxcsys = $this->session->userdata('jxcsys');
    }

    public function index()
    {
        $action = $this->input->get('action', TRUE);
        switch($action)
        {
            case 'initSo':
                $this->common_model->checkpurview(189);
                $this->load->view('scm/invSo/initSo');
                break;
            case 'editSo':
                $this->common_model->checkpurview(190);
                $this->load->view('scm/invSo/initSo');
                break;
            case 'initSoList':
                $this->common_model->checkpurview(188);
                $this->load->view('scm/invSo/initSoList');
                break;
            default:
                $this->common_model->checkpurview(188);
                $this->soList();
        }
    }

    public function soList()
    {
        $page = max(intval($this->input->get_post('page', TRUE)), 1);
        $rows = max(intval($this->input->get_post('rows', TRUE)), 100);
        $sidx = str_enhtml($this->input->get_post('sidx', TRUE));
        $sord = str_enhtml($this->input->get_post('sord', TRUE));
        $transType = intval($this->input->get_post('transType', TRUE));
        $hxState = intval($this->input->get_post('hxState', TRUE));
        $salesId = intval($this->input->get_post('salesId', TRUE));
        $matchCon = str_enhtml($this->input->get_post('matchCon', TRUE));
        $beginDate = str_enhtml($this->input->get_post('beginDate', TRUE));
        $endDate = str_enhtml($this->input->get_post('endDate', TRUE));
        $order = $sidx ? $sidx . ' ' . $sord : ' a.id desc';
        $where = 'a.isDelete=0 and a.billType="SALE"';
        $where .= $transType ? ' and a.transType=' . $transType : '';
        $where .= $salesId > 0 ? ' and a.salesId=' . $salesId : '';
        $where .= $matchCon ? ' and a.postData like "%' . $matchCon . '%"' : '';
        $where .= $beginDate ? ' and a.billDate>="' . $beginDate . '"' : '';
        $where .= $endDate ? ' and a.billDate<="' . $endDate . '"' : '';
        $where .= $this->common_model->get_admin_purview();
        $offset = $rows * ($page - 1);
        $list = $this->data_model->get_order($where . ' order by ' . $order . ' limit ' . $offset . ',' . $rows);
        foreach($list as $arr => $row)
        {
            $v[$arr]['checkName'] = $row['checkName'];
            $v[$arr]['checkTime'] = $row['checkTime'];
            $v[$arr]['checked'] = intval($row['checked']);
            $v[$arr]['salesId'] = intval($row['salesId']);
            $v[$arr]['salesName'] = $row['salesName'];
            $v[$arr]['billDate'] = $row['billDate'];
            $v[$arr]['deliveryDate'] = $row['deliveryDate'];
            $v[$arr]['billStatus'] = intval($row['billStatus']);
            $v[$arr]['billStatusName'] = get_order_status($row);

            $v[$arr]['totalQty'] = (float)$row['totalQty'];
            $v[$arr]['id'] = intval($row['id']);
            $v[$arr]['amount'] = (float)abs($row['amount']);

            $v[$arr]['transType'] = intval($row['transType']);
            $v[$arr]['rpAmount'] = (float)abs($row['rpAmount']);
            $v[$arr]['contactName'] = $row['contactName'];
            $v[$arr]['description'] = $row['description'];
            $v[$arr]['billNo'] = $row['billNo'];
            $v[$arr]['totalAmount'] = (float)abs($row['totalAmount']);
            $v[$arr]['userName'] = $row['userName'];
            $v[$arr]['transTypeName'] = $row['transTypeName'];
        }
        $data['status'] = 200;
        $data['msg'] = 'success';
        $data['data']['page'] = $page;
        $data['data']['records'] = $this->data_model->get_order($where, 3);
        $data['data']['total'] = ceil($data['data']['records'] / $rows);
        $data['data']['rows'] = isset($v) ? $v : array();
        die(json_encode($data));
    }

    /**
     * 导出
     * @author longli
     */
    public function exportInvSo()
    {
        $this->common_model->checkpurview(192);
        $name = 'sales_order_' . date('YmdHis') . '.xls';
        sys_csv($name);
        $this->common_model->logs('导出销售订单单据:' . $name);
        $sidx = str_enhtml($this->input->get_post('sidx', TRUE));
        $sord = str_enhtml($this->input->get_post('sord', TRUE));
        $transType = intval($this->input->get_post('transType', TRUE));
        $hxState = intval($this->input->get_post('hxState', TRUE));
        $salesId = intval($this->input->get_post('salesId', TRUE));
        $matchCon = str_enhtml($this->input->get_post('matchCon', TRUE));
        $beginDate = str_enhtml($this->input->get_post('beginDate', TRUE));
        $endDate = str_enhtml($this->input->get_post('endDate', TRUE));
        $order = $sidx ? $sidx . ' ' . $sord : ' a.id desc';
        $where = 'a.isDelete=0 and a.billType="SALE"';
        $where .= $transType ? ' and a.transType=' . $transType : '';
        $where .= $salesId > 0 ? ' and a.salesId=' . $salesId : '';
        $where .= $matchCon ? ' and a.postData like "%' . $matchCon . '%"' : '';
        $where .= $beginDate ? ' and a.billDate>="' . $beginDate . '"' : '';
        $where .= $endDate ? ' and a.billDate<="' . $endDate . '"' : '';
        $where .= $this->common_model->get_admin_purview();
        $data['list'] = $this->data_model->get_order($where . ' order by ' . $order);
        $this->load->view('scm/invSo/exportInvSo', $data);
    }

    /**
     * 新增
     * @author longli
     */
    public function add()
    {
        $this->common_model->checkpurview(189);
        $data = $this->input->post('postData', TRUE);
        if(strlen($data) > 0)
        {
            $data = $this->validform((array)json_decode($data, true));
            $info = elements(array(
                'billNo', 'billType', 'transType', 'transTypeName', 'buId', 'arrears',
                'billDate', 'description', 'totalQty', 'amount', 'rpAmount', 'totalAmount',
                'hxStateCode', 'totalArrears', 'disRate', 'disAmount', 'customerFree', 'postData',
                'salesId', 'uid', 'userName', 'accId', 'deliveryDate', 'modifyTime'), $data);
            $this->db->trans_begin();
            $iid = $this->mysql_model->insert('order', $info);
            $this->invso_info($iid, $data);
            if($this->db->trans_status() === FALSE)
            {
                $this->db->trans_rollback();
                str_alert(-1, 'SQL错误或者提交的是空数据');
            }
            else
            {
                $this->db->trans_commit();
                $this->common_model->logs('新增客户 订单编号：' . $data['billNo']);
                str_alert(200, 'success', array('id' => intval($iid)));
            }
        }
        str_alert(-1, '提交的是空数据');
    }

    /**
     * 新增
     * @author longli
     */
    public function addNew()
    {
        $this->add();
    }

    /**
     * 修改
     * @author longli
     */
    public function updateinvSo()
    {
        $this->common_model->checkpurview(190);
        $data = $this->input->post('postData', TRUE);
        if(strlen($data) > 0)
        {
            $data = $this->validform((array)json_decode($data, true));
            $info = elements(array(
                'billNo', 'billType', 'transType', 'transTypeName', 'buId', 'arrears',
                'billDate', 'description', 'totalQty', 'amount', 'rpAmount', 'totalAmount',
                'hxStateCode', 'totalArrears', 'disRate', 'disAmount', 'customerFree', 'postData',
                'salesId', 'uid', 'userName', 'accId', 'deliveryDate', 'modifyTime'), $data);
            $this->db->trans_begin();
            $this->mysql_model->update('order', $info, '(id=' . $data['id'] . ')');
            $this->invso_info($data['id'], $data);
            if($this->db->trans_status() === FALSE)
            {
                $this->db->trans_rollback();
                str_alert(-1, 'SQL错误或者提交的是空数据');
            }
            else
            {
                $this->db->trans_commit();
                $this->common_model->logs('修改客户 订单编号：' . $data['billNo']);
                str_alert(200, 'success', array('id' => $data['id']));
            }
        }
        str_alert(-1, '提交的数据不为空');
    }

    /**
     * 获取修改信息
     * @author longli
     */
    public function update()
    {
        $this->common_model->checkpurview();
        $id = intval($this->input->get_post('id', TRUE));
        $data = $this->data_model->get_order('a.isDelete=0 and a.id=' . $id . ' and a.billType="SALE"', 1);
        if(count($data) > 0)
        {
            $info['status'] = 200;
            $info['msg'] = 'success';
            $info['data']['id'] = intval($data['id']);
            $info['data']['buId'] = intval($data['buId']);
            $info['data']['cLevel'] = 0;
            $info['data']['deliveryDate'] = $data['deliveryDate'];
            $info['data']['contactName'] = $data['contactName'];
            $info['data']['salesId'] = intval($data['salesId']);
            $info['data']['date'] = $data['billDate'];
            $info['data']['billNo'] = $data['billNo'];
            $info['data']['billType'] = $data['billType'];
            $info['data']['transType'] = intval($data['transType']);
            $info['data']['totalQty'] = (float)$data['totalQty'];
            $info['data']['modifyTime'] = $data['modifyTime'];
            $info['data']['checkName'] = $data['checkName'];
            $info['data']['disRate'] = (float)$data['disRate'];
            $info['data']['billStatus'] = intval($data['billStatus']);
            $info['data']['disAmount'] = (float)$data['disAmount'];
            $info['data']['amount'] = (float)abs($data['amount']);
            $info['data']['rpAmount'] = (float)abs($data['rpAmount']);
            $info['data']['customerFree'] = (float)$data['customerFree'];
            $info['data']['arrears'] = (float)abs($data['arrears']);
            $info['data']['userName'] = $data['userName'];
            $info['data']['checked'] = intval($data['checked']);
            $info['data']['status'] = intval($data['checked']) == 1 ? 'view' : 'edit'; //edit
            $info['data']['totalDiscount'] = (float)$data['totalDiscount'];
            $info['data']['totalAmount'] = (float)abs($data['totalAmount']);
            $info['data']['description'] = $data['description'];
            //add by longli 20190116 begin
            $info['data']['weight'] = $data['weight'];
            $info['data']['traceNumOne'] = $data['traceNumOne'];
            $info['data']['channel'] = $data['channel'];
            $info['data']['traceNumTwo'] = $data['traceNumTwo'];
            $info['data']['channelCost'] = $data['channelCost'];
            //add by longli 20190116 end
            $list = $this->data_model->get_order_info('a.isDelete=0 and a.iid=' . $id . ' order by a.id');
            foreach($list as $arr => $row)
            {
                $v[$arr]['invSpec'] = $row['invSpec'];
                $v[$arr]['taxRate'] = (float)$row['taxRate'];
                $v[$arr]['srcOrderEntryId'] = intval($row['srcOrderEntryId']);
                $v[$arr]['srcOrderNo'] = $row['srcOrderNo'];
                $v[$arr]['srcOrderId'] = intval($row['srcOrderId']);
                $v[$arr]['goods'] = $row['invNumber'] . ' ' . $row['invName'] . ' ' . $row['invSpec'];
                $v[$arr]['invName'] = $row['invName'];
                $v[$arr]['qty'] = (float)abs($row['qty']);
                $v[$arr]['locationName'] = $row['locationName'];
                $v[$arr]['amount'] = (float)abs($row['amount']);
                $v[$arr]['taxAmount'] = (float)$row['taxAmount'];
                $v[$arr]['price'] = (float)$row['price'];
                $v[$arr]['tax'] = (float)$row['tax'];
                $v[$arr]['mainUnit'] = $row['mainUnit'];
                $v[$arr]['deduction'] = (float)$row['deduction'];
                $v[$arr]['invId'] = intval($row['invId']);
                $v[$arr]['invNumber'] = $row['invNumber'];
                $v[$arr]['locationId'] = intval($row['locationId']);
                $v[$arr]['locationName'] = $row['locationName'];
                $v[$arr]['discountRate'] = $row['discountRate'];
                $v[$arr]['description'] = $row['description'];

                $v[$arr]['unitId'] = intval($row['unitId']);
                $v[$arr]['mainUnit'] = $row['mainUnit'];
            }

            $info['data']['entries'] = isset($v) ? $v : array();
            $info['data']['accId'] = (float)$data['accId'];

            $info['data']['accounts'] = array();
            die(json_encode($info));
        }
        str_alert(-1, '单据不存在、或者已删除');
    }


    public function queryDetails()
    {
        $this->common_model->checkpurview();
        $id = intval($this->input->get_post('id', TRUE));
        $data = $this->data_model->get_order('a.isDelete=0 and a.id=' . $id . ' and a.billType="SALE"', 1);
        if(count($data) > 0)
        {
            $data['billStatus'] == 2 && str_alert(400, '订单 ' . $data['billNo'] . ' 已全部出库，不能生成购货单！');
            $info['status'] = 200;
            $info['msg'] = 'success';
            $info['data']['id'] = intval($data['id']);
            $info['data']['buId'] = intval($data['buId']);
            $info['data']['cLevel'] = 0;
            $info['data']['deliveryDate'] = $data['deliveryDate'];
            $info['data']['contactName'] = $data['contactName'];
            $info['data']['salesId'] = intval($data['salesId']);
            $info['data']['date'] = $data['billDate'];
            $info['data']['billNo'] = $data['billNo'];
            $info['data']['billType'] = $data['billType'];
            $info['data']['transType'] = intval($data['transType']);
            $info['data']['totalQty'] = (float)$data['totalQty'];
            $info['data']['modifyTime'] = $data['modifyTime'];
            $info['data']['checkName'] = $data['checkName'];
            $info['data']['disRate'] = (float)$data['disRate'];
            $info['data']['disAmount'] = (float)$data['disAmount'];
            $info['data']['amount'] = (float)abs($data['amount']);
            $info['data']['rpAmount'] = (float)abs($data['rpAmount']);
            $info['data']['customerFree'] = (float)$data['customerFree'];
            $info['data']['arrears'] = (float)abs($data['arrears']);
            $info['data']['userName'] = $data['userName'];
            $info['data']['status'] = intval($data['checked']) == 1 ? 'view' : 'edit'; //edit
            $info['data']['totalDiscount'] = (float)$data['totalDiscount'];
            $info['data']['totalAmount'] = (float)abs($data['totalAmount']);
            $list = $this->data_model->get_order_info('a.isDelete=0 and a.iid=' . $id . ' order by a.id');
            foreach($list as $arr => $row)
            {
                $v[$arr]['invSpec'] = $row['invSpec'];
                $v[$arr]['taxRate'] = (float)$row['taxRate'];
                $v[$arr]['srcOrderEntryId'] = 1;
                $v[$arr]['srcOrderNo'] = $data['billNo'];
                $v[$arr]['srcOrderId'] = intval($id);
                $v[$arr]['goods'] = $row['invNumber'] . ' ' . $row['invName'] . ' ' . $row['invSpec'];
                $v[$arr]['invName'] = $row['invName'];
                $v[$arr]['qty'] = (float)abs($row['qty']);
                $v[$arr]['locationName'] = $row['locationName'];
                $v[$arr]['amount'] = (float)abs($row['amount']);
                $v[$arr]['taxAmount'] = (float)$row['taxAmount'];
                $v[$arr]['price'] = (float)$row['price'];
                $v[$arr]['tax'] = (float)$row['tax'];
                $v[$arr]['mainUnit'] = $row['mainUnit'];
                $v[$arr]['deduction'] = (float)$row['deduction'];
                $v[$arr]['invId'] = intval($row['invId']);
                $v[$arr]['invNumber'] = $row['invNumber'];
                $v[$arr]['locationId'] = intval($row['locationId']);
                $v[$arr]['locationName'] = $row['locationName'];
                $v[$arr]['discountRate'] = $row['discountRate'];
                $v[$arr]['description'] = $row['description'];

                $v[$arr]['unitId'] = intval($row['unitId']);
                $v[$arr]['mainUnit'] = $row['mainUnit'];
            }

            $info['data']['entries'] = isset($v) ? $v : array();
            $info['data']['accId'] = (float)$data['accId'];

            $info['data']['accounts'] = array();
            die(json_encode($info));
        }
        str_alert(-1, '单据不存在、或者已删除');
    }


    /**
     * 打印
     * @author longli
     */
    public function toPdf()
    {
        $this->common_model->checkpurview(193);
        $id = intval($this->input->get('id', TRUE));
        $data = $this->data_model->get_order('a.isDelete=0 and a.id=' . $id . ' and a.billType="SALE"', 1);
        if(count($data) > 0)
        {
            if($data['checked'] == 0)
                str_alert(-1, "订单未审核不能打印发货单");
            $this->mysql_model->update('order', array('billStatus' => 11), array('id' => $id));
            $this->common_model->logs('打印发货单：' . $data['billNo']);
            $data['num'] = 8;
            $data['system'] = $this->common_model->get_option('system');
            $postData = unserialize($data['postData']);
            foreach($postData['entries'] as $arr => $row)
            {
                $v[$arr]['i'] = $arr + 1;
                $v[$arr]['invId'] = intval($row['invId']);
                $v[$arr]['invNumber'] = $row['invNumber'];
                $v[$arr]['invSpec'] = $row['invSpec'];
                $v[$arr]['invName'] = $row['invName'];
                $v[$arr]['goods'] = $row['invNumber'] . ' ' . $row['invName'] . ' ' . $row['invSpec'];
                $v[$arr]['qty'] = (float)abs($row['qty']);
                $v[$arr]['price'] = $row['price'];
                $v[$arr]['mainUnit'] = $row['mainUnit'];
                $v[$arr]['amount'] = $row['amount'];
                $v[$arr]['deduction'] = $row['deduction'];
                $v[$arr]['discountRate'] = $row['discountRate'];
                $v[$arr]['locationName'] = $row['locationName'];
            }
            $data['countpage'] = ceil(count($postData['entries']) / $data['num']);;
            $data['list'] = isset($v) ? $v : array();
            ob_start();
            $this->load->view('scm/invSo/toPdf', $data);
            $content = ob_get_clean();
            require_once('./application/libraries/html2pdf/html2pdf.php');
            try
            {
                $html2pdf = new HTML2PDF('P', 'A4', 'en');
                $html2pdf->setDefaultFont('javiergb');
                $html2pdf->pdf->SetDisplayMode('fullpage');
                $html2pdf->writeHTML($content, '');
                $html2pdf->Output('invSo_' . date('ymdHis') . '.pdf');
            }
            catch(HTML2PDF_exception $e)
            {
                echo $e;
                exit;
            }
        }
        str_alert(-1, '单据不存在、或者已删除');
    }

    /**
     * 删除
     * @author longli
     */
    public function delete()
    {
        $this->common_model->checkpurview(191);
        $id = intval($this->input->get('id', TRUE));
        $data = $this->mysql_model->get_rows('order', '(id=' . $id . ') and billType="SALE"');
        if(count($data) > 0)
        {
            $data['checked'] > 0 && str_alert(-1, '已审核的不可删除');
            $this->db->trans_begin();
            $this->mysql_model->delete('order', '(id=' . $id . ')');
            $this->mysql_model->delete('order_info', '(iid=' . $id . ')');
            if($this->db->trans_status() === FALSE)
            {
                $this->db->trans_rollback();
                str_alert(-1, '删除失败');
            }
            else
            {
                $this->db->trans_commit();
                $this->common_model->logs('删除客户订单编号：' . $data['billNo']);
                str_alert(200, 'success');
            }
        }
        str_alert(-1, '单据不存在、或者已删除');
    }

    public function batchClose()
    {
        str_alert(-1, '暂无此功能');
    }

    /**
     * 批量审核
     * @author longli
     */
    public function batchcheckinvSo()
    {
        $this->common_model->checkpurview(194);
        $id = str_enhtml($this->input->post('id', TRUE));
        $data = $this->mysql_model->get_results('order', '(id in(' . $id . ')) and billType="SALE" and checked=0 and isDelete=0');
        if(count($data) > 0)
        {
            $this->mysql_model->get_count('invoice', '(srcOrderId in(' . $id . ')) and isDelete=0') > 0 && str_alert(-1, '有关联的销货单，不能对它进行审核！');
            $billno = [];
            $retMsg = "";
            foreach($data as $arr => $row)
            {
                $postData = unserialize($row['postData']);
                $checkRet = $this->check_order_stock($postData);
                if(empty($checkRet))
                {
                    $isUpdate = $this->mysql_model->update('order', array('checked' => 1, 'checkName' => $this->jxcsys['name'], 'checkTime' => date('Y-m-d H:i:s')), "(id = {$row['id']})");
                    if(!$isUpdate)
                    {
                        str_alert(-1, '审核失败');
                    }
                    $billno[] = $row['billNo'];
                    $this->orderLockStock($postData);
                }
                else
                {
                    $logMsg = "";
                    foreach($checkRet as $goodsId => $goods)
                    {
                        $logMsg .= "( {$goods['invNumber']} {$goods['invName']} 缺少库存 {$goods['invPcs']}个 )";
                        $retMsg .= "订单号 {$postData['billNo']} $logMsg <br/>";
                    }
                    $this->common_model->logs("订单号 {$postData['billNo']} $logMsg");
                }
            }
            $tos = "";
            if(!empty($billno))
            {
                $tos = join(',', $billno);
                $this->common_model->logs("客户订单编号: $tos 的单据已被审核！");
            }
            if(empty($retMsg))
            {
                str_alert(200, "订单编号：$billno 的单据已被审核！");
            }
            else
            {
                !empty($tos) && $tos = "订单号 $tos 审核成功 <br/>";
                str_alert(-1, "$tos $retMsg");
            }
        }
        str_alert(-1, '所选的单据都已被审核，请选择未审核的单据进行审核！');
    }

    /**
     * 批量反审核
     * @author longli
     */
    public function rsbatchcheckinvSo()
    {
        $this->common_model->checkpurview(195);
        $id = str_enhtml($this->input->post('id', TRUE));
        // 释放锁定库存
        $this->orderReleaseStock($id);
        $data = $this->mysql_model->get_results('order', '(id in(' . $id . ')) and billType="SALE" and checked=1 and (isDelete=0)');
        if(count($data) > 0)
        {
            $this->mysql_model->get_count('invoice', '(srcOrderId in(' . $id . ')) and isDelete=0') > 0 && str_alert(-1, '有关联的销货单，不能对它进行反审核！');
            $sql = $this->mysql_model->update('order', array('billStatus' => -1, 'checked' => 0, 'checkName' => '', 'checkTime' => null), '(id in(' . $id . '))');
            if($sql)
            {
                $billno = array_column($data, 'billNo', 'id');
                $billno = join(',', $billno);
                $this->common_model->logs('客户订单：' . $billno . '的单据已被反审核！');
                str_alert(200, '订单编号：' . $billno . '的单据已被反审核！');
            }
            str_alert(-1, '反审核失败');
        }
        str_alert(-1, '所选的订单都是未审核，请选择已审核的订单进行反审核！');
    }

    /**
     * 单个审核
     * @author longli
     */
    public function checkInvSo()
    {
        $this->common_model->checkpurview(194);
        $data = $this->input->post('postData', TRUE);
        if(strlen($data) > 0)
        {
            $data = $this->validform((array)json_decode($data, true));
            $this->mysql_model->get_count('invoice', array('srcOrderId' => $data['id'], 'isDelete' => 0)) > 0 && str_alert(-1, '有关联的销货单，不能对它进行反审核！');
            $data['checked'] = 1;
            $data['checkName'] = $this->jxcsys['name'];
            $data['checkTime'] = date('Y-m-d H:i:s');
            $info = elements(array(
                'billNo', 'billType', 'transType', 'transTypeName', 'buId', 'checked', 'checkName', 'checkTime',
                'billDate', 'description', 'totalQty', 'amount', 'rpAmount', 'totalAmount', 'customerFree', 'arrears',
                'hxStateCode', 'totalArrears', 'disRate', 'disAmount', 'postData',
                'salesId', 'accId', 'deliveryDate', 'modifyTime'), $data);
            // 验证库存
            $chekRet = $this->check_order_stock($data);
            if(!empty($chekRet))
            {
                $retMsg = '';
                foreach($chekRet as $goods)
                {
                    $retMsg .= "{$goods['invNumber']} {$goods['invName']} 缺少库存 {$goods['invPcs']}个 <br/>";
                }
                str_alert(-1, $retMsg);
            }
            $this->db->trans_begin();
            // 锁定库存
            $this->orderLockStock($data);
            //特殊情况
            if($data['id'] < 0)
            {
                $info = elements(array(
                    'billNo', 'billType', 'transType', 'transTypeName', 'buId', 'checked', 'checkTime', 'arrears',
                    'billDate', 'description', 'totalQty', 'amount', 'rpAmount', 'totalAmount', 'customerFree',
                    'hxStateCode', 'totalArrears', 'disRate', 'disAmount', 'checkName', 'postData',
                    'salesId', 'uid', 'userName', 'accId', 'deliveryDate', 'modifyTime'), $data, NULL);
                $iid = $this->mysql_model->insert('order', $info);
                $this->invso_info($iid, $data);
                $data['id'] = $iid;
            }
            else
            {
                $this->mysql_model->update('order', $info, array('id' => $data['id']));
                $this->invso_info($data['id'], $data);
            }
            if($this->db->trans_status() === FALSE)
            {
                $this->db->trans_rollback();
                str_alert(-1, 'SQL错误或者提交的是空数据');
            }
            else
            {
                $this->db->trans_commit();
                $this->common_model->logs('客户单编号：' . $data['billNo'] . '的单据已被审核！');
                str_alert(200, 'success', array('id' => $data['id']));
            }
        }
        str_alert(-1, '审核失败');
    }

    /**
     * 单个反审核
     * @author longli
     */
    public function revsCheckInvSo()
    {
        $this->common_model->checkpurview(195);
        $data = $this->input->post('postData', TRUE);
        if(strlen($data) > 0)
        {
            $data = $this->validform((array)json_decode($data, true));
            // 解锁库存
            $this->orderReleaseStock($data['id']);
            $this->mysql_model->get_count('invoice', array('srcOrderId' => $data['id'], 'isDelete' => 0)) > 0 && str_alert(-1, '有关联的销货单，不能对它进行反审核！');
            $sql = $this->mysql_model->update('order', array('billStatus' => -1, 'checked' => 0, 'checkName' => '', 'checkTime' => null), '(id=' . $data['id'] . ')');
            if($sql)
            {
                $this->common_model->logs('客户订单：' . $data['billNo'] . '的单据已被反审核！');
                str_alert(200, 'success', array('id' => $data['id']));
            }
        }
        str_alert(-1, '提交失败');
    }


    public function findNearSoEmp()
    {
        die('{"status":200,"msg":"success","data":{"empId":0}}');
    }

    /**
     * 公共验证
     * @param array $data 验证数据
     * @return mixed
     * @author longli
     */
    private function validform($data)
    {
        $data['id'] = isset($data['id']) ? intval($data['id']) : 0;
        $data['buId'] = intval($data['buId']);
        $data['salesId'] = intval($data['salesId']);
        $data['billType'] = 'SALE';
        $data['billDate'] = $data['date'];
        $data['transType'] = intval($data['transType']);
        $data['transTypeName'] = $data['transType'] == 150601 ? '订货' : '退货';
        $data['description'] = $data['description'];
        $data['totalQty'] = (float)$data['totalQty'];
        $data['totalTax'] = isset($data['totalTax']) ? (float)$data['totalTax'] : 0;
        $data['totalTaxAmount'] = isset($data['totalTaxAmount']) ? (float)$data['totalTaxAmount'] : 0;
        $data['amount'] = $data['transType'] == 150601 ? abs($data['amount']) : -abs($data['amount']);
        $data['totalAmount'] = $data['transType'] == 150601 ? abs($data['totalAmount']) : -abs($data['totalAmount']);
        $data['disRate'] = (float)$data['disRate'];
        $data['disAmount'] = (float)$data['disAmount'];
        $data['totalDiscount'] = (float)$data['totalDiscount'];
        $data['uid'] = $this->jxcsys['uid'];
        $data['userName'] = $this->jxcsys['name'];
        $data['modifyTime'] = date('Y-m-d H:i:s');

        //修改的时候
        if($data['id'] > 0)
        {
            $invoice = $this->mysql_model->get_rows('order', array('id' => $data['id'], 'billType' => 'SALE', 'isDelete' => 0));
            count($invoice) < 1 && str_alert(-1, '单据不存在、或者已删除');
            $data['checked'] = $invoice['checked'];
            $data['billNo'] = $invoice['billNo'];
        }
        else
        {
            $data['billNo'] = str_no('XSDD');
        }

        $data['disRate'] < 0 && str_alert(-1, '折扣率要为数字，请输入有效数字！');
        abs($data['amount']) < abs($data['disAmount']) && str_alert(-1, '折扣额不能大于合计金额！');

        //数据验证
        if(is_array($data['entries']))
        {
            count($data['entries']) < 1 && str_alert(-1, '提交的是空数据');
        }
        else
        {
            str_alert(-1, '提交的是空数据');
        }

        //供应商验证
        $this->mysql_model->get_count('contact', '(id=' . intval($data['buId']) . ')') < 1 && str_alert(-1, '客户不存在');

        //商品录入验证
        $storage = array_column($this->mysql_model->get_results('storage', '(disable=0)'), 'id');
        foreach($data['entries'] as $arr => $row)
        {
            intval($row['invId']) < 1 && str_alert(-1, '请选择商品');
            (float)$row['qty'] < 0 && str_alert(-1, '商品数量要为数字，请输入有效数字！');
            (float)$row['price'] < 0 && str_alert(-1, '商品销售单价要为数字，请输入有效数字！');
            (float)$row['discountRate'] < 0 && str_alert(-1, '折扣率要为数字，请输入有效数字！');
            intval($row['locationId']) < 1 && str_alert(-1, '请选择相应的仓库！');
            !in_array($row['locationId'], $storage) && str_alert(-1, $row['locationName'] . '不存在或不可用！');
        }
        $data['postData'] = serialize($data);
        return $data;

    }


    /**
     * 组装数据
     * @param $iid
     * @param $data
     * @author longli
     */
    private function invso_info($iid, $data)
    {
        $transType = $this->data_model->get_invoce_type_list('out');
        foreach($data['entries'] as $arr => $row)
        {
            $v[$arr]['iid'] = $iid;
            $v[$arr]['billNo'] = $data['billNo'];
            $v[$arr]['billDate'] = $data['billDate'];
            $v[$arr]['buId'] = $data['buId'];
            $v[$arr]['transType'] = $data['transType'];
            $v[$arr]['transTypeName'] = $data['transTypeName'];
            $v[$arr]['billType'] = $data['billType'];
            $v[$arr]['salesId'] = $data['salesId'];
            $v[$arr]['invId'] = intval($row['invId']);
            $v[$arr]['skuId'] = intval($row['skuId']);
            $v[$arr]['unitId'] = intval($row['unitId']);
            $v[$arr]['locationId'] = intval($row['locationId']);
            $v[$arr]['qty'] = array_key_exists($data['transType'], $transType) ? -abs($row['qty']) : abs($row['qty']);
            // $v[$arr]['qty']           = $data['transType']==150601 ? -abs($row['qty']) :abs($row['qty']);
            $v[$arr]['amount'] = array_key_exists($data['transType'], $transType) ? abs($row['amount']) : -abs($row['amount']);
            // $v[$arr]['amount']        = $data['transType']==150601 ? abs($row['amount']) :-abs($row['amount']);
            $v[$arr]['price'] = abs($row['price']);
            $v[$arr]['discountRate'] = $row['discountRate'];
            $v[$arr]['deduction'] = $row['deduction'];
            $v[$arr]['description'] = $row['description'];
            $v[$arr]['uid'] = $data['uid'];
        }
        if(isset($v))
        {
            if(isset($data['id']) && $data['id'] > 0)
            {
                $this->mysql_model->delete('order_info', '(iid=' . $iid . ')');
            }
            $this->mysql_model->insert('order_info', $v);
        }
    }

    /**
     * 审核订单锁定库存
     * @param array $orderData 订单数据
     * @date   2019/01/09
     * @author longli
     */
    private function orderLockStock($orderData = [])
    {
        $this->wms_model->orderLockStock($orderData['id']);
    }

    /**
     * 反审核订单释放库存
     * @param string $orderIds 订单 id 多个使用英文逗号分开
     * @date   2019/01/09
     * @author longli
     */
    private function orderReleaseStock($orderIds = "")
    {
        $this->wms_model->orderReleaseStock($orderIds);
    }

    /**
     * 检查订单是否有足够的库存
     * @param array $postData    订单详情
     * @param bool  $isLockStock 是否锁定库存，默认为不锁定
     * @return array
     * @date   2019/01/09
     * @author longli
     */
    private function check_order_stock($postData = [], $isLockStock = false)
    {
        $ret = [];
        $args = [
            'invId' => array_column($postData['entries'], 'invId'),
            'locationId' => array_column($postData['entries'], 'locationId'),
            'is_wms' => true
        ];
        $wmsStockList = $this->data_model->get_invoice_info_inventory($args);
        foreach($postData['entries'] as $entry)
        {
            $qty = intval($entry['qty']);
            $wmsStock = $wmsStockList[$entry['locationId']][$entry['invId']];
            if($wmsStock < $qty)
            {
                $ret[$entry['invId']] = [
                    // 返回缺少库存的个数
                    'invPcs' => $qty - $wmsStock, // 缺少的个数
                    'invName' => $entry['invName'], // 产品名称
                    'invNumber' => $entry['invNumber'], // 产品编号
                ];
            }
        }
        return $ret;
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */